//
//  DefinitionsDelegate.m
//  Words2
//
//  Created by Mathieu Tozer on 14/09/06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "DefinitionsDelegate.h"
#import "DefinitionMO.h"
#import "WordsAppDelegate.h"
#import "Blob.h"


@implementation DefinitionsDelegate

-(NSString *)blobCountInfo{
	return [NSString stringWithFormat:@"%d of %d", [blobArrayController selectionIndex], [[blobArrayController arrangedObjects] count]];
}

-(IBAction)setMainDefinition:(id)sender{
	NSAttributedString *definition;
	definition = [definitionTextBlob attributedSubstringFromRange:[definitionTextBlob selectedRange]];
	
	//convert to regular NSString (might support attributed strings in the future)
	NSString *plainString = [definition string];
	[[[wordArrayController selectedObjects] objectAtIndex:0] setValue:plainString forKey:@"mainDef"];
	//[mainDefinitionField setStringValue:plainString]; //these aren't being saved into the word...
}
-(IBAction)setSecondaryDefinition:(id)sender{
	NSAttributedString *definition;
	definition = [definitionTextBlob attributedSubstringFromRange:[definitionTextBlob selectedRange]];
	
	//convert to regular NSString (might support attributed strings in the future)
	NSString *plainString = [definition string];
	[[[wordArrayController selectedObjects] objectAtIndex:0] setValue:plainString forKey:@"secondaryDef"];
	//[secondaryDefinitionField setStringValue:plainString];
}
-(IBAction)setWordType:(id)sender{
	NSAttributedString *type;
	type = [definitionTextBlob attributedSubstringFromRange:[definitionTextBlob selectedRange]];
	
	//convert to regular NSString (might support attributed strings in the future)
	NSString *plainString = [type string];
	[[[wordArrayController selectedObjects] objectAtIndex:0] setValue:plainString forKey:@"wordType"];
//	[wordTypeField setStringValue:plainString];
}

//get the rank from the button itself
//creates a new definition if there is nothing selected and there is text selected in the text blob, and sets it to the ranking
-(IBAction)newDefinitionForRank:(id)sender{
	NSString *ranking = [sender title];
	NSNumber *rankNum = [NSNumber numberWithInt:[ranking intValue]];
	
	NSAttributedString *definition;
	definition = [definitionTextBlob attributedSubstringFromRange:[definitionTextBlob selectedRange]]; 
	
	//convert to regular NSString (might support attributed strings in the future) nah prolly not a good idea
	NSString *plainString = [definition string];
	
	//if there's a ranking which is the same as the one that I'm trying to set in the array right now, then replace it
	NSArray *existingDefs = [definitionsArrayController arrangedObjects];
	NSEnumerator *e = [existingDefs objectEnumerator];
	DefinitionMO *nextDef;
	while(nextDef = [e nextObject]){
		if([[nextDef valueForKey:@"rank"] isEqualToNumber:rankNum]){
			[nextDef setValue:plainString forKey:@"definition"];
			//keep these updated now for the sake of quizzing.
			if([rankNum intValue] == 1){
				[[[wordArrayController selectedObjects] objectAtIndex:0] setValue:plainString forKey:@"mainDef"];
			}
			if([rankNum intValue] == 2){
				[[[wordArrayController selectedObjects] objectAtIndex:0] setValue:plainString forKey:@"secondaryDef"];
			}
			return;
			//update and return
		}
	}
	NSLog(@"New One");

	//else create a new one.
	DefinitionMO *def = [NSEntityDescription insertNewObjectForEntityForName:@"Definition" inManagedObjectContext:[[NSApp delegate] managedObjectContext]];
	[def setValue:[[wordArrayController selectedObjects] objectAtIndex:0] forKey:@"word"];
	[def setValue:[[[NSApp delegate] currentlySelectedTransLanguage] valueForKey:@"capability"] forKey:@"capabilityKey"];

	[def setValue:rankNum forKey:@"rank"];
	NSLog(@"rank: %@", rankNum);
	
	[def setValue:plainString forKey:@"definition"];
	
	//keep these updated now for the sake of quizzing.
	if([rankNum intValue] == 1){
		[[[wordArrayController selectedObjects] objectAtIndex:0] setValue:plainString forKey:@"mainDef"];
	}
	if([rankNum intValue] == 2){
		[[[wordArrayController selectedObjects] objectAtIndex:0] setValue:plainString forKey:@"secondaryDef"];
	}
	[definitionsArrayController addObject:def];
}


-(IBAction)addDefinition:(id)sender{
	DefinitionMO *def = [NSEntityDescription insertNewObjectForEntityForName:@"Definition" inManagedObjectContext:[[NSApp delegate] managedObjectContext]];
	[def setValue:[[wordArrayController selectedObjects] objectAtIndex:0] forKey:@"word"];
	[def setValue:[[[NSApp delegate] currentlySelectedTransLanguage] valueForKey:@"capability"] forKey:@"capabilityKey"]; //set the capabilityKey to whatever is selected.
	[definitionsArrayController addObject:def];
	[definitionsArrayController setSelectedObjects:[NSArray arrayWithObject:def]];

}

-(IBAction)addBlob:(id)sender{
	Blob *newBlob = [NSEntityDescription insertNewObjectForEntityForName:@"Blob" inManagedObjectContext:[[NSApp delegate] managedObjectContext]];
	[newBlob setValue:[[wordArrayController selectedObjects] objectAtIndex:0] forKey:@"word"];
	[newBlob setValue:[[[NSApp delegate] currentlySelectedTransLanguage] valueForKey:@"capability"] forKey:@"capabilityKey"]; //set the capabilityKey to whatever is selected.
	[blobArrayController addObject:newBlob];
	[blobArrayController setSelectedObjects:[NSArray arrayWithObject:newBlob]];
//	[blobArrayController insertObject:newBlob atArrangedObjectIndex:0];

}

-(IBAction)removeBlob:(id)sender{
	Blob *blobToRemove = [[blobArrayController selectedObjects] objectAtIndex:0];
	[[[NSApp delegate] managedObjectContext] deleteObject:blobToRemove];
	[blobArrayController removeObject:blobToRemove];
}



@end
